package run;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import networkTraining.IDataSet;
import networkTraining.IDataSetProvider;
import networkTraining.INetworkTrainer;
import networkTraining.NetworkTrainer;
import networkTraining.RandomizedDataSetProvider;
import networkTraining.algorithms.ITrainingAlgorithm;
import networkTraining.algorithms.LinearOuputLayerTrainer;
import networkTraining.convergence.IConvergenceTest;
import networkTraining.convergence.WindowConvergenceTest;
import networkTraining.epoch.IEpochExecutor;
import networkTraining.epoch.IncrementalSOMTrainingEpoch;
import neuralNetwork.INeuralNetwork;
import neuralNetwork.builder.RadialNetworkBuilder;

public class HW6Main
{

	public static void main(String[] args) throws IOException
	{
		RadialNetworkBuilder networkBuilder = new RadialNetworkBuilder();
		IDataSet trainingDataSet = TrainingDataParser.parseTrainingData(new File("src/run/TrainingData.txt"));
		IDataSetProvider dataSetProvider = new RandomizedDataSetProvider(trainingDataSet, null, 7);

		List<double[]> centers = buildCenterList(625);
		INeuralNetwork neuralNetwork = networkBuilder.buildNetwork(2, 625, centers);

		INetworkTrainer networkTrainer = new NetworkTrainer();
		networkTrainer.setNeuralNetwork(neuralNetwork);
		networkTrainer.setDataSetProvider(dataSetProvider);

		IEpochExecutor epochExecutor = new IncrementalSOMTrainingEpoch(1, 1000, 1000, 5);
		networkTrainer.setEpochExecutor(epochExecutor);

		//ITrainingAlgorithm trainingAlgorithm = new LinearOuputLayerTrainer(neuralNetwork, .01);
		//networkTrainer.setTrainingAlgorithm(trainingAlgorithm);

		//IConvergenceTest convergenceTest = new WindowConvergenceTest(10);
		//networkTrainer.setConvergenceTest(convergenceTest);

		networkTrainer.trainNetwork();

		// System.out.println(epochTrainingErrorInspector.getInspectionInfo());
		// System.out.println(epochTestingErrorInspector.getInspectionInfo());

	}

	private static List<double[]> buildCenterList(int centerCount)
	{
		int dim = (int) Math.sqrt(centerCount);

		double x = 0;
		double y = 0;

		List<double[]> centers = new ArrayList<double[]>();
		for (int i = 0; i < dim; i++)
		{
			x += 1.0/dim;
			y = 0;
			for (int j = 0; j < dim; j++)
			{
				y += 1.0/dim;
				double[] center =
				{ x, y };
				centers.add(center);
			}

		}

		return centers;
	}

}
